
cc.Class({
    extends: cc.Component,

    properties: {
        pokerFly:{
            default:null,
            type:cc.AudioClip
        },

        notice:{
            default:null,
            type:cc.AudioClip
        },

        turn:{
            default:null,
            type:cc.AudioClip
        },

        kapai:{
            default:null,
            type:cc.AudioClip
        },

        coin:{
            default:null,
            type:cc.AudioClip
        }
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        cc.audioEngine.setEffectsVolume(1)

        this.node.on("sound_on",this._soundOn.bind(this),this)
        this.node.on("sound_off",this._soundOff.bind(this),this)
        this.node.on("vibration_on",this._vibrationOn.bind(this),this)
        this.node.on("vibration_off",this._vibrationOff.bind(this),this)

        let soundsOn = cc.sys.localStorage.getItem("user_setting_music_on")
        let vibrationOn = cc.sys.localStorage.getItem("user_setting_vibration_on")

        if(!soundsOn || (soundsOn != cc.gg.enumConfig.SETTING.ON && soundsOn != cc.gg.enumConfig.SETTING.OFF)){
            cc.log("没有本地数据，设置音效为开")
            this._soundOn()
        }else {
            this.soundsOn = parseInt(soundsOn)
        }

        if(!vibrationOn || (vibrationOn != cc.gg.enumConfig.SETTING.ON && vibrationOn != cc.gg.enumConfig.SETTING.OFF)){
            cc.log("没有本地数据，设置震动为开")
            this._vibrationOn()
        }else {
            this.vibrationOn = parseInt(vibrationOn)
        }
        cc.log("当前音量开关：",this.soundsOn)
        cc.log("当前震动开关：",this.vibrationOn)
    },

    start () {

    },

    // update (dt) {},

    _soundOn:function () {
        cc.sys.localStorage.setItem("user_setting_music_on",cc.gg.enumConfig.SETTING.ON)
        cc.warn("sound on option here")
        this.soundsOn = cc.gg.enumConfig.SETTING.ON
    },

    _soundOff:function () {
        cc.sys.localStorage.setItem("user_setting_music_on",cc.gg.enumConfig.SETTING.OFF)
        cc.warn("sound off option here")
        this.soundsOn = cc.gg.enumConfig.SETTING.OFF
    },

    _vibrationOn:function () {
        cc.sys.localStorage.setItem("user_setting_vibration_on",cc.gg.enumConfig.SETTING.ON)
        this.vibrationOn = cc.gg.enumConfig.SETTING.ON
    },

    _vibrationOff:function () {
        cc.sys.localStorage.setItem("user_setting_vibration_on",cc.gg.enumConfig.SETTING.OFF)
        this.vibrationOn = false
    },

    playTurnNoticeVibration:function () {
        if(this.vibrationOn === cc.gg.enumConfig.SETTING.ON) cc.gg.baseUtil.vibrate(500)
    },

    playTurnStartSound:function () {
        if(this.soundsOn === cc.gg.enumConfig.SETTING.OFF) return
        cc.audioEngine.playEffect(this.notice, false)
    },

    playTimeNoticeSound:function () {
        if(this.soundsOn === cc.gg.enumConfig.SETTING.OFF) return
        cc.audioEngine.playEffect(this.turn, false)
    },

    playPokerFlySound:function () {
        if(this.soundsOn === cc.gg.enumConfig.SETTING.OFF) return
        cc.audioEngine.playEffect(this.pokerFly, false)
    },

    playKaPaiSound:function () {
        if(this.soundsOn === cc.gg.enumConfig.SETTING.OFF) return
        cc.audioEngine.playEffect(this.kapai, false)
    },

    playCoinSound:function () {
        if(this.soundsOn === cc.gg.enumConfig.SETTING.OFF) return
        cc.audioEngine.playEffect(this.coin, false)
    },

    playBGM(audioUrl,isLoop)
    {
        cc.pp.resourceUtil.loadFromPath(audioUrl,function (obj) {
            let audioClip = obj
            var id = cc.audioEngine.playMusic(audioClip, isLoop)
        })
    },

    stopBGM()
    {
        cc.audioEngine.stopMusic();
    },


});
